package com.wanmait.recruit.controller;

import com.wanmait.recruit.pojo.*;
import com.wanmait.recruit.service.*;
import com.wanmait.recruit.util.CookieUtil;
import com.wanmait.recruit.util.MailUtil;
import com.wanmait.recruit.util.Md5Util;
import org.apache.commons.io.FilenameUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/web/person")
public class PersonController {
    @Resource
    PersonService personService;
    @Resource
    ResumeService resumeService;
    @Resource
    EducationExpService educationExpService;
    @Resource
    EducationalBackgroundTypeService educationalBackgroundTypeService;
    @Resource
    SchoolService schoolService;
    @Resource
    PositionDesiredService positionDesiredService;
    @Resource
    PositionTypeService positionTypeService;
    @Resource
    PositionService positionService;
    @Resource
    IndustryService industryService;
    @Resource
    CityService cityService;
    @Resource
    ProjectExpService projectExpService;
    @Resource
    WorkExpService workExpService;
    @Resource
    SkillLabelInfoService skillLabelInfoService;
    @Resource
    SkillLabelService skillLabelService;
    @Resource
    PersonSystemInfoService personSystemInfoService;

    @RequestMapping("/checkEmail")
    public void checkEmail(String email, HttpServletResponse response)throws IOException {
        Person person = personService.checkEmail(email);
        PrintWriter pw = response.getWriter();
        if(person!=null){
            pw.print(0);
        }
        else{
            pw.print(1);
        }
    }

    @RequestMapping("/checkName")
    public void checkName(String personName,HttpServletResponse response)throws IOException{
        Person person = personService.checkPersonName(personName);
        PrintWriter pw = response.getWriter();
        if(person!=null){
            pw.print(0);
        }
        else{
            pw.print(1);
        }
    }

    @RequestMapping("/signUp")
    public String signUp(Person person, HttpServletRequest request){
        try {
            request.setCharacterEncoding("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        //System.out.println(person.toString());
        //生成激活码并加密
        String code = "";
        for(int i=0;i<5;i++){

            code += (int)(Math.random()*10);

        }

        Md5Hash md5Hash = new Md5Hash(code);
        person.setCode(md5Hash.toString());

        new Thread(new MailUtil(person.getEmail(),code)).start();;

        personService.insert(person);
        String source = request.getHeader("referer");
        source = source.substring(source.indexOf("/web/"));

        return "redirect:/web/index?msg=activation";
    }

    @RequestMapping("/check")
    public String checkEmail(String code){

        Md5Hash md5Hash = new Md5Hash(code);
        Person person = personService.checkEmailByCode(md5Hash.toString());
        //验证通过，需要给用户发送系统消息。
        if(person!=null){
            //System.out.println("验证成功！！！");
            PersonSystemInfo personSystemInfo = new PersonSystemInfo();
            personSystemInfo.setPersonId(person.getId());
            personSystemInfo.setContent("恭喜你，注册成功，欢迎使用本网站");
            personSystemInfoService.insert(personSystemInfo);
            return "/web/checkEmailSuccess";
        }
        return "/web/checkEmailFail";
    }

    @PostMapping("personLogin")
    public ModelAndView personLogin(String personName, String password, String rememberMe, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//用户使用用户名密码登录
        ModelAndView modelAndView = new ModelAndView();
        boolean personRegister = personService.isPersonRegister(personName, password);
        if (personRegister){
            Person person = personService.isPersonRegisterData(personName, password);
            HttpSession session = request.getSession();
            session.setAttribute("person", person);
            //检查是否记住我
            if (rememberMe!=null){//写cookie
                CookieUtil.addCookie("personName", person.getPersonName(), "/", 30*24*60*60, response);
                password= Md5Util.md5Super(password);//加密
                CookieUtil.addCookie("password", password, "/", 30*24*60*60, response);
            }
            else {//"删除"cookie
                CookieUtil.addCookie("personName", "", "/", 0, response);
                CookieUtil.addCookie("password", "", "/", 0, response);
            }
            String source = request.getHeader("referer");
            source = source.substring(source.indexOf("/web/"));
            modelAndView.setViewName("redirect:"+source);
        }
        else {
            String err="登录名或密码错误,请重新输入!";
            modelAndView.addObject("err",err);
            String source = request.getHeader("referer");
            source = source.substring(source.indexOf("/web/"));
            modelAndView.setViewName("redirect:"+source);
        }
        return modelAndView;
    }

    @RequestMapping("exit")
    public String exit(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        session.removeAttribute("person");
        //删除cookie
        CookieUtil.addCookie("personName","","/",0,response);
        CookieUtil.addCookie("password","","/",0,response);
        String source = request.getHeader("referer");
        source = source.substring(source.indexOf("/web/"));
        return "redirect:"+source;
    }
    @RequestMapping("personInfo")
    public String personInfo(){return "web/personInfo";}
    @RequestMapping("personInfoUpdate")
    public String personInfoUpdate(
            Integer personId1, String name1, String personSex1,
            String personAge1, String personPhone1,HttpServletRequest request
    )throws ServletException, IOException
    {
        Person person = new Person();
        person.setId(personId1);
        person.setName(name1);
        person.setSex(personSex1);

        java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
        Date date =null;
        try {
            date =  formatter.parse(personAge1);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        person.setBirth(date);
        person.setPhone(personPhone1);

        personService.updatePersonInfo(person);

        HttpSession session = request.getSession();
        Person person1=(Person)session.getAttribute("person");

        Person person2=new Person();
        person2.setId(person1.getId());
        person2.setPersonName(person1.getPersonName());
        person2.setPassword(person1.getPassword());
        person2.setEmail(person1.getEmail());
        person2.setPic(person1.getPic());
        person2.setName(name1);
        person2.setSex(personSex1);
        person2.setBirth(date);
        person2.setPhone(personPhone1);
        session.setAttribute("person", person2);

        return "redirect:/web/index";
    }
    //personPic
    @RequestMapping("personPic")
    public String personPic(){return "/web/personPic";}
    @PostMapping("personPicUpLoad")
    public ModelAndView personPicUpLoad(Integer personId, MultipartFile personPic, HttpServletRequest request) {
        //System.out.println(personId);
        //System.out.println(personPic);
        ModelAndView modelAndView=new ModelAndView();
        Person person=null;
        if (!personPic.isEmpty()){
            //物理路径
            String realpath=request.getServletContext().getRealPath("/static/images/web/uploadimages");
            //原文件名
            String originalFileName=personPic.getOriginalFilename();
            //获得扩展名
            String ext= FilenameUtils.getExtension(originalFileName);
            //文件大小
            //long size = file.getSize();
            //唯一文件名
            String personPicName= UUID.randomUUID() +"."+ ext;

            File outPersonPicName=new File(realpath,personPicName);
            person=new Person();
            person.setId(personId);
            //保存
            try{
                personPic.transferTo(outPersonPicName);
                person.setPic(personPicName);
            }catch (IOException e){
                e.printStackTrace();
            }
        }
        personService.updatePersonPic(person);
        HttpSession session = request.getSession();
        Person person1=(Person)session.getAttribute("person");
        person1.setPic(person.getPic());
        modelAndView.setViewName("redirect:/web/index");
        return modelAndView;
    }
    @RequestMapping("personChangePassword")
    public String personChangePassword(){return "/web/personChangePassword";}
    @RequestMapping("changePersonNameAndPassword")
    public String changePersonNameAndPassword(Integer personId,String myname,String password,HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Person person = new Person();
        person.setId(personId);
        person.setPersonName(myname);
        person.setPassword(password);
        personService.updatePersonNameAndPassword(person);

        HttpSession session = request.getSession();
        session.removeAttribute("person");
        //删除cookie
        CookieUtil.addCookie("personName","","/",0,response);
        CookieUtil.addCookie("password","","/",0,response);

        return "redirect:/web/index";
    }
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·resume+++++++++++++++*/
   /*
   * 简历涉及的表有:
   * resume简历表  资格证书credentials   个人优势advantages
   * 学历education            学历类型表educational_background_type
   * education_exp教育经历表          educational_background_type_id学历类型
   *
   * position_desired期望职位表
   * project_exp项目经历
   * work_exp工作经历
   * skill_label_info用户技能标签信息表  resume id没有person id
   *资格证书
   * */

    //判断个人简历个数进入页面
    @RequestMapping("personResumeNum")
    public ModelAndView  personResumeNum(HttpServletRequest request){
        HttpSession session = request.getSession();
        Person person = (Person) session.getAttribute("person");
        Integer personId=person.getId();
        List<Resume> resumes=resumeService.findByPersonId(personId);
        ModelAndView modelAndView =new ModelAndView();
        modelAndView.addObject("resumes",resumes);
        modelAndView.setViewName("/web/personResumeNum");
        return modelAndView;
    }
    @RequestMapping("personResumeSetResumeId")//resume简历  id  放到session
    @ResponseBody
    public void personResumeSetResumeId(Integer resumeId,HttpServletRequest request){
        //System.out.println(resumeId);
        HttpSession session = request.getSession();
        session.setAttribute("resumeId", resumeId);
    }
    @RequestMapping("personResume")//resume简历
    public ModelAndView personResume(Integer resumeId,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        Person person = (Person) session.getAttribute("person");
        Integer personId=person.getId();

       /* if (resumeId==null){
            Integer resumeId1=(Integer) session.getAttribute("resumeId");
            if (resumeId1==null){
                //注册一个简历
                Resume resume = new Resume();
                resume.setPersonId(personId);
                resumeService.insert(resume);
                //获取最新的简历id
                resumeId1=resumeService.findNewId();
                //放到session
                session.setAttribute("resumeId", resumeId1);
            }
            resumeId=resumeId1;
        }*/
        if (resumeId==null){
            //注册一个简历
            Resume resume = new Resume();
            resume.setPersonId(personId);
            resumeService.insert(resume);
            //获取最新的简历id
            resumeId=resumeService.findNewId();
            //放到session
            session.setAttribute("resumeId", resumeId);
        }
        modelAndView.addObject("resumeId",resumeId);
        //System.out.println(resumeId);

        //advantages个人优势
        boolean isAdvantage = resumeService.findIsAdvantage(resumeId);
        if (isAdvantage==false){
            modelAndView.addObject("advantages","");
        }
        else {
            String advantages = resumeService.findAdvantages(resumeId);//个人优势
            //System.out.println(advantages);
            if ("0".equals(advantages)){advantages=null;}
            modelAndView.addObject("advantages",advantages);
        }

        //Credentials证书
        boolean isCredentials = resumeService.findIsCredentials(resumeId);
        if (isCredentials==false){
            modelAndView.addObject("credentials","");
        }
        else {
            String credentials = resumeService.findCredentials(resumeId);
            //System.out.println(credentials);
            if ("0".equals(credentials)){credentials=null;}
            modelAndView.addObject("credentials",credentials);
        }

        //学历
        boolean isEducation = educationExpService.findIsEducation(resumeId);
        if (isEducation==false){
            modelAndView.addObject("education","");
        }
        else {
            String education = educationExpService.findEducation(resumeId);
            modelAndView.addObject("education",education);
        }

        //学历类型
        ArrayList<EducationalBackgroundType> educationalBackgroundTypes = educationalBackgroundTypeService.selectFindAll();
        modelAndView.addObject("educationalBackgroundTypes",educationalBackgroundTypes);


        //教育经历  Educational experience
        boolean isEducationalExperience = educationExpService.findIsEducationalExperience(resumeId);
        if (isEducationalExperience==false){
            modelAndView.addObject("educationalExperience","");
        }
        else {
            ArrayList<EducationExp> educationalExperience = educationExpService.findEducationalExperience(resumeId);
            modelAndView.addObject("educationalExperience",educationalExperience);
        }

        //学校
        ArrayList<School> allSchool = schoolService.selectFindAll();
        modelAndView.addObject("allSchool",allSchool);

        //期望职位  position_desired
        boolean isPositionDesired = positionDesiredService.findIsPositionDesired(resumeId);
        if (isPositionDesired==false){
            modelAndView.addObject("positionDesired",null);
        }
        else {
            PositionDesired positionDesired = positionDesiredService.findPositionDesired(resumeId);
            modelAndView.addObject("positionDesired",positionDesired);
        }

        //获取职位类型值positionTypeId
        ArrayList<PositionType> allPositionType=positionTypeService.selectFindAll();
        modelAndView.addObject("allPositionType",allPositionType);

        //获取职位名称positionId
        ArrayList<Position> allPosition=positionService.selectFindAll();
        modelAndView.addObject("allPosition",allPosition);

        //获取行业值industryId
        ArrayList<Industry> allindustry=industryService.selectFindAll();
        modelAndView.addObject("allindustry",allindustry);

        //获取城市cityId
        ArrayList<City> allcity=cityService.selectFindAll();
        modelAndView.addObject("allcity",allcity);

        //项目经历 project_exp
        boolean isProjectExp = projectExpService.findIsProjectExp(resumeId);
        if (isProjectExp==false){
            modelAndView.addObject("projectExp","");
        }
        else {
            ArrayList<ProjectExp> projectExp = projectExpService.findProjectExp(resumeId);
            modelAndView.addObject("projectExp",projectExp);
        }

        //工作经历 work_exp
        boolean isWorkExp = workExpService.findIsWorkExp(resumeId);
        if (isWorkExp==false){
            modelAndView.addObject("workExp","");
        }
        else {
            ArrayList<WorkExp> workExp = workExpService.findWorkExp(resumeId);
            modelAndView.addObject("workExp",workExp);
        }

        //个人技能标签
        boolean isSkillLabelInfo = skillLabelInfoService.findIsSkillLabelInfo(resumeId);
        if (isSkillLabelInfo==false){
            modelAndView.addObject("skillLabelInfo","");
        }
        else {
            List<SkillLabelInfo> skillLabelInfo=skillLabelInfoService.findSkillLabelInfo(resumeId);
            modelAndView.addObject("skillLabelInfo",skillLabelInfo);
        }
        //技能标签
        List<SkillLabel> skillLabels=skillLabelService.findAll();
        modelAndView.addObject("skillLabels",skillLabels);

        modelAndView.setViewName("/web/personResume");

        return modelAndView;
    }
    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·个人优势·advantages+++++++++++++++*/
    @RequestMapping("personAdvantage")
    @ResponseBody
    public void personAdvantage(Integer id,String advantages,Integer personId){
        //System.out.println(id);
        //System.out.println(advantages);
        boolean isAdvantage = resumeService.findIsAdvantage(id);
        if (isAdvantage){
            //System.out.println("更新");
            resumeService.updateAdvantage(id, advantages);
        }
        else {
            //System.out.println("插入");
            resumeService.insertAdvantage(personId,advantages);
        }
    }
    @RequestMapping("personDel1")
    @ResponseBody
    public void personDel1(Integer id){
        //System.out.println(id);
        resumeService.delAdvantage(id);
    }
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·资格证书++++++++++*/
    @RequestMapping("personCredentials")
    @ResponseBody
    public void personCredentials(Integer id,String credentials,Integer personId){
    //System.out.println(id);
    //System.out.println(credentials);
    boolean isCredentials = resumeService.findIsCredentials(id);
    if (isCredentials){
        resumeService.updateCredentials(id, credentials);
    }
    else {
        resumeService.insertCredentials(personId,credentials);
    }
}
    @RequestMapping("personDel2")
    @ResponseBody
    public void personDel2(Integer id){
        //System.out.println(id);
        resumeService.delCredentials(id);
    }
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·学历++++++++++*/
    @RequestMapping("personEducation")
    @ResponseBody
    public void personEducation(Integer educationtypeId,Integer personId){
        //System.out.println(educationtypeId);
        //System.out.println(personId);
        boolean isEducation = educationExpService.findIsEducation(personId);
        if (isEducation){
            educationExpService.updateEducation(personId, educationtypeId);
        }
        else {
            educationExpService.insertEducation(personId,educationtypeId);
        }
    }
    @RequestMapping("personDel3")
    @ResponseBody
    public void personDel3(Integer id){
        //System.out.println(id);
        educationExpService.delEducation(id);
    }
    /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·教育经历++++++++++*/
    @RequestMapping("getSchoolName")
    @ResponseBody
    public void getSchoolName(Integer id, HttpServletResponse response){
        //System.out.println(id);
        if (id==null){return;}
        String s = schoolService.selectNameById(id);
        //String str= JSON.toJSONString(s);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("getEduTypeName")
    @ResponseBody
    public void getEduTypeName(Integer id, HttpServletResponse response){
        //System.out.println(id);
        String s = educationalBackgroundTypeService.selectByEducationTypeId(id);
        //String str= JSON.toJSONString(s);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("personDel4")
    @ResponseBody
    public void personDel4(Integer id){
        //System.out.println(id);
        educationExpService.delEducationExp(id);
    }
    @RequestMapping("personEducationExp")
    @ResponseBody
    public void personEducationExp(
            Integer personId,Integer schoolName,String startTime,String graduationTime,
            String associationActivity, Integer isFullTime,String professionalName,Integer selectEducation,
            HttpServletRequest request
    ){
        HttpSession session = request.getSession();
        Integer resumeId=(Integer) session.getAttribute("resumeId");
        java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
        Date dateStartTime =null;
        Date dateGraduationTime =null;
        try {
            dateStartTime =  formatter.parse(startTime);
            dateGraduationTime =  formatter.parse(graduationTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        EducationExp educationExp =new EducationExp();
        educationExp.setPersonId(personId);
        educationExp.setSchoolId(schoolName);
        educationExp.setStartTime(dateStartTime);
        educationExp.setGraduationTime(dateGraduationTime);
        educationExp.setAssociationActivity(associationActivity);
        boolean isFullTimeX=true;if (isFullTime==0){isFullTimeX=false;}
        educationExp.setIsFullTime(isFullTimeX);
        educationExp.setProfessionalName(professionalName);
        educationExp.setEducationalBackgroundTypeId(selectEducation);
        educationExp.setResumeId(resumeId);

        educationExpService.insert(educationExp);

    }

    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·期望职位++++++++++*/

    @RequestMapping("getPositionTypeName")
    @ResponseBody
    public void getPositionTypeName(Integer positionTypeId, HttpServletResponse response){
        //System.out.println(positionTypeId);
        if (positionTypeId==null){return;}
        String s = positionTypeService.selectByPositionTypeId(positionTypeId);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("getPositionName")
    @ResponseBody
    public void getPositionName(Integer positionId, HttpServletResponse response){
        //System.out.println(positionTypeId);
        if (positionId==null){return;}
        String s = positionService.selectByPositionTypeId(positionId);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("getIndustryName")
    @ResponseBody
    public void getIndustryName(Integer industryId, HttpServletResponse response){
        //System.out.println(positionTypeId);
        if (industryId==null){return;}
        String s = industryService.selectByPositionTypeId(industryId);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("getCityName")
    @ResponseBody
    public void getCityName(Integer cityId, HttpServletResponse response){
        //System.out.println(positionTypeId);
        if (cityId==null){return;}
        String s = cityService.selectByPositionTypeId(cityId);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("personDel5")
    @ResponseBody
    public void personDel5(Integer id){
        //System.out.println(id);
        positionDesiredService.delEducationExp(id);
    }

    @RequestMapping("personPositionDesired")
    @ResponseBody
    public void personPositionDesired(
            Integer personId,Integer positionTypeId,Integer positionId,
            Integer minp,Integer maxp,Integer industryId,Integer cityId, HttpServletRequest request
    ){
        HttpSession session = request.getSession();
        Integer resumeId=(Integer) session.getAttribute("resumeId");
        PositionDesired positionDesired=new PositionDesired();
            positionDesired.setPersonId(personId);
            positionDesired.setPositionTypeId(positionTypeId);
            positionDesired.setPositionId(positionId);
            positionDesired.setSalaryMin(minp);
            positionDesired.setSalaryMax(maxp);
            positionDesired.setIndustryId(industryId);
            positionDesired.setCityId(cityId);
            positionDesired.setResumeId(resumeId);

            boolean isPositionDesired = positionDesiredService.findIsPositionDesired(personId);
            if (isPositionDesired==false){
                positionDesiredService.insert(positionDesired);
            }
            else {
                positionDesiredService.update(positionDesired);
            }

    }
    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·项目经历++++++++++*/
    @RequestMapping("personDel6")
    @ResponseBody
    public void personDel6(Integer id){
        //System.out.println(id);
        projectExpService.delProjectExp(id);
    }

    @RequestMapping("personProjectExp")
    @ResponseBody
    public void personProjectExp(
            Integer personId,String proExpName,String proExpRole,String proExpUrl,String sT1,
            String gT1,String proExpDescribe,String proExpAchievement,HttpServletRequest request
    ){
        HttpSession session = request.getSession();
        Integer resumeId=(Integer) session.getAttribute("resumeId");
        java.text.SimpleDateFormat formatter = new SimpleDateFormat( "yyyy-MM-dd");
        Date dateStartTime =null;
        Date dateEndTime =null;
        try {
            dateStartTime =  formatter.parse(sT1);
            dateEndTime =  formatter.parse(gT1);
            //int i = dateStartTime.compareTo(dateEndTime);

        } catch (ParseException e) {
            e.printStackTrace();
        }
        ProjectExp projectExp=new ProjectExp();
        projectExp.setPersonId(personId);
        projectExp.setName(proExpName);
        projectExp.setRole(proExpRole);
        projectExp.setUrl(proExpUrl);
        projectExp.setStartTime(dateStartTime);
        projectExp.setEndTime(dateEndTime);
        projectExp.setDescribe(proExpDescribe);
        projectExp.setAchievement(proExpAchievement);
        projectExp.setResumeId(resumeId);
        projectExpService.insert(projectExp);
    }
    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·工作经历++++++++++*/
    @RequestMapping("personDel7")
    @ResponseBody
    public void personDel7(Integer id){
        //System.out.println(id);
        //projectExpService.delProjectExp(id);
        workExpService.delWorkExp(id);
    }
    @RequestMapping("personWorkExp")
    @ResponseBody
    public void personWorkExp(
            Integer personId,Integer positionTypeId,Integer positionId,
            boolean isPractice,String companyName,Integer industryId,String department,
            Integer workTime,String workContent,String workAchievement,HttpServletRequest request
    ){
        HttpSession session = request.getSession();
        Integer resumeId=(Integer) session.getAttribute("resumeId");
        WorkExp workExp=new WorkExp();
        workExp.setPersonId(personId);
        workExp.setPositionTypeId(positionTypeId);
        workExp.setPositionId(positionId);
        workExp.setIsPractice(isPractice);
        workExp.setCompanyName(companyName);
        workExp.setIndustryId(industryId);
        workExp.setDepartment(department);
        workExp.setWorkTime(workTime);
        workExp.setWorkContent(workContent);
        workExp.setWorkAchievement(workAchievement);
        workExp.setResumeId(resumeId);

        workExpService.insert(workExp);
    }

    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历·技能标签++++++++++*/
    //resumeId  简历
    @RequestMapping("getSkillLabelName")
    @ResponseBody
    public void getSkillLabelName(Integer skillLabelId, HttpServletResponse response){
        //System.out.println(positionTypeId);
        if (skillLabelId==null){return;}
        String s =skillLabelService.findNameById(skillLabelId);
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = null;
        try {
            writer = response.getWriter();
        } catch (IOException e) {
            e.printStackTrace();
        }
        writer.print(s);
        writer.close();

    }
    @RequestMapping("personDel8")
    @ResponseBody
    public void personDel8(Integer id){
        //System.out.println(id);
        skillLabelInfoService.deleteById(id);
    }
    @RequestMapping("personSkillLabelInfo")
    @ResponseBody
    public void personSkillLabelInfo(String ids,HttpServletRequest request){
        //System.out.println(ids);
        HttpSession session = request.getSession();
        Integer resumeId = (Integer) session.getAttribute("resumeId");
        String[] split = ids.split(",");
        //System.out.println(resumeId);
        //先删除resumeI下的所有skillLabelId
        skillLabelInfoService.deleteByResumeId(resumeId);

        for (int i=0;i<split.length;i++){
            String s = split[i];
            Integer skillLabelId = Integer.valueOf(s);
            //System.out.println(skillLabelId);
            //在直接添加resumeI下选的所有skillLabelId
            SkillLabelInfo skillLabelInfo=new SkillLabelInfo();
            skillLabelInfo.setEnable(true);
            skillLabelInfo.setResumeId(resumeId);
            skillLabelInfo.setSkillLabelId(skillLabelId);
            skillLabelInfoService.insert(skillLabelInfo);
        };

    }

    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++简历预览++++++++++++++++++++++++*/
    @RequestMapping("personResumeGenerate")//resume简历
    public ModelAndView personResumeGenerate(Integer resumeId,HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView();
        HttpSession session = request.getSession();
        Person person = (Person) session.getAttribute("person");
        Integer personId=person.getId();

        if (resumeId==null){
            Integer resumeId1=(Integer) session.getAttribute("resumeId");
            if (resumeId1==null){
                //注册一个简历
                Resume resume = new Resume();
                resume.setPersonId(personId);
                resumeService.insert(resume);
                //获取最新的简历id
                resumeId1=resumeService.findNewId();
                //放到session
                session.setAttribute("resumeId", resumeId1);
            }
            resumeId=resumeId1;
        }
        modelAndView.addObject("resumeId",resumeId);
        //System.out.println(resumeId);

        //advantages个人优势
        boolean isAdvantage = resumeService.findIsAdvantage(resumeId);
        if (isAdvantage==false){
            modelAndView.addObject("advantages","");
        }
        else {
            String advantages = resumeService.findAdvantages(resumeId);//个人优势
            //System.out.println(advantages);
            if ("0".equals(advantages)){advantages=null;}
            modelAndView.addObject("advantages",advantages);
        }

        //Credentials证书
        boolean isCredentials = resumeService.findIsCredentials(resumeId);
        if (isCredentials==false){
            modelAndView.addObject("credentials","");
        }
        else {
            String credentials = resumeService.findCredentials(resumeId);
            //System.out.println(credentials);
            if ("0".equals(credentials)){credentials=null;}
            modelAndView.addObject("credentials",credentials);
        }

        //学历
        boolean isEducation = educationExpService.findIsEducation(resumeId);
        if (isEducation==false){
            modelAndView.addObject("education","");
        }
        else {
            String education = educationExpService.findEducation(resumeId);
            modelAndView.addObject("education",education);
        }

        //学历类型
        ArrayList<EducationalBackgroundType> educationalBackgroundTypes = educationalBackgroundTypeService.selectFindAll();
        modelAndView.addObject("educationalBackgroundTypes",educationalBackgroundTypes);



        //教育经历  Educational experience
        boolean isEducationalExperience = educationExpService.findIsEducationalExperience(resumeId);
        if (isEducationalExperience==false){
            modelAndView.addObject("educationalExperience","");
        }
        else {
            ArrayList<EducationExp> educationalExperience = educationExpService.findEducationalExperience(resumeId);
            modelAndView.addObject("educationalExperience",educationalExperience);
        }

        //学校
        ArrayList<School> allSchool = schoolService.selectFindAll();
        modelAndView.addObject("allSchool",allSchool);

        //期望职位  position_desired
        boolean isPositionDesired = positionDesiredService.findIsPositionDesired(resumeId);
        if (isPositionDesired==false){
            modelAndView.addObject("positionDesired",null);
        }
        else {
            PositionDesired positionDesired = positionDesiredService.findPositionDesired(resumeId);
            modelAndView.addObject("positionDesired",positionDesired);
        }

        //获取职位类型值positionTypeId
        ArrayList<PositionType> allPositionType=positionTypeService.selectFindAll();
        modelAndView.addObject("allPositionType",allPositionType);

        //获取职位名称positionId
        ArrayList<Position> allPosition=positionService.selectFindAll();
        modelAndView.addObject("allPosition",allPosition);

        //获取行业值industryId
        ArrayList<Industry> allindustry=industryService.selectFindAll();
        modelAndView.addObject("allindustry",allindustry);

        //获取城市cityId
        ArrayList<City> allcity=cityService.selectFindAll();
        modelAndView.addObject("allcity",allcity);

        //项目经历 project_exp
        boolean isProjectExp = projectExpService.findIsProjectExp(resumeId);
        if (isProjectExp==false){
            modelAndView.addObject("projectExp","");
        }
        else {
            ArrayList<ProjectExp> projectExp = projectExpService.findProjectExp(resumeId);
            modelAndView.addObject("projectExp",projectExp);
        }

        //工作经历 work_exp
        boolean isWorkExp = workExpService.findIsWorkExp(resumeId);
        if (isWorkExp==false){
            modelAndView.addObject("workExp","");
        }
        else {
            ArrayList<WorkExp> workExp = workExpService.findWorkExp(resumeId);
            modelAndView.addObject("workExp",workExp);
        }

        //个人技能标签
        boolean isSkillLabelInfo = skillLabelInfoService.findIsSkillLabelInfo(resumeId);

        if (isSkillLabelInfo==false){
            modelAndView.addObject("skillLabelInfo","");
        }
        else {
            ArrayList<SkillLabelInfo> skillLabelInfo=skillLabelInfoService.findSkillLabelInfo(resumeId);
            //skillLabelInfo.forEach(skillLabelInfo1 -> System.out.println(skillLabelInfo1.toString()));
            modelAndView.addObject("skillLabelInfo",skillLabelInfo);
        }

        //技能标签
        List<SkillLabel> skillLabels=skillLabelService.findAll();
        modelAndView.addObject("skillLabels",skillLabels);

        modelAndView.setViewName("/web/personResumeGenerate");

        return modelAndView;
    }


    /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/

}